package com.hospital.pharmacy.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.dao.InventoryMapper;
import com.hospital.pharmacy.dao.MedicineMapper;
import com.hospital.pharmacy.dao.SalesRecordMapper;
import com.hospital.pharmacy.entity.Inventory;
import com.hospital.pharmacy.entity.Medicine;
import com.hospital.pharmacy.entity.SalesRecord;
import com.hospital.pharmacy.service.SaleRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: Hospital
 * @description: 销售实现
 * @author: 杨曼珍
 * @create: 2019-01-07 11:29
 **/
@Service
public class SaleRecordServiceImpl implements SaleRecordService {
    @Autowired
    SalesRecordMapper salesRecordMapper;
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    MedicineMapper medicineMapper;

    //显示查找到的所有销售记录
    public Map getAll(SalesRecord salesRecord) {
        Map<String, String> map;

        Inventory inventory = inventoryMapper.selectByPrimaryKey(salesRecord.getSalRecIvtrId());
        Medicine medicine = medicineMapper.selectByPrimaryKey(inventory.getIvtrMdcId());
        String mdc_name = medicine.getMdcName();
        String mdc_unit = medicine.getMdcUnit();
        String mdc_spec = medicine.getMdcSpec();


        map = new HashMap<>();
        map.put("id", salesRecord.getId().toString());
        map.put("mdcSpec", mdc_spec);
        map.put("salRecTime", new SimpleDateFormat("yyyy-MM-dd").format(salesRecord.getSalRecTime()));
        map.put("salRecCount", salesRecord.getSalRecCount().toString());
        map.put("salRecPrice", salesRecord.getSalRecPrice().toString());
        map.put("salRecTotal", salesRecord.getSalRecTotal().toString());
        map.put("mdcUnit", mdc_unit);
        map.put("mdcName", mdc_name);


        return map;

    }


    @Override
    //显示所有销售记录
    public String getAllSale() {
        List<SalesRecord> salesRecords = salesRecordMapper.selectAllSale();
        List<Map<String, String>> resaultmap = new ArrayList<>();
        for (SalesRecord salesRecord : salesRecords) {
            resaultmap.add(getAll(salesRecord));
        }
        return JSON.toJSONString(resaultmap);
    }

    @Override
    //销售
    public String saleMed(Integer sale_med_id, Date sale_med_time, Integer sale_med_count, Double sale_med_price, Double sale_med_total, String sale_remark) {
        try {
            Inventory inventory = inventoryMapper.selectByMdcId(sale_med_id);
            if (inventory.getIvtrCount() - sale_med_count < 0) {
                return JSON.toJSONString("库存不足");
            }
            inventory.setIvtrCount(inventory.getIvtrCount() - sale_med_count);
            inventoryMapper.updateByPrimaryKey(inventory);
            System.out.println(inventory);
            SalesRecord salesRecord = new SalesRecord();
            salesRecord.setSalRecIvtrId(sale_med_id);
            salesRecord.setSalRecTime(sale_med_time);
            salesRecord.setSalRecCount(sale_med_count);
            salesRecord.setSalRecPrice(sale_med_price);
            salesRecord.setSalRecTotal(sale_med_total);
            salesRecord.setSalRecRemark(sale_remark);
            salesRecordMapper.insertSelective(salesRecord);
            //如果最小库存大于等于实际库存
            if (inventory.getIvtrLimit() >= inventory.getIvtrCount()) {
                return JSON.toJSONString("库存不足");
            } else
                return getAllSale();
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("出库出错");
        }


    }

    //根据药品名称查找售货记录
    @Override
    public String selectSaleByMedName(String med_name) {
        try {
            List<Medicine> medicines = medicineMapper.selectByMdcName(med_name);
            List<Map<String, String>> resaultmap = new ArrayList<>();
            for (Medicine medicine : medicines) {

                Inventory inventory = inventoryMapper.selectByMdcId(medicine.getId());
                List<SalesRecord> salesRecords = salesRecordMapper.selectSaleByIvtrId(inventory.getId());
                for (SalesRecord salesRecord : salesRecords) {
                    resaultmap.add(getAll(salesRecord));
                }
            }
            System.out.println(resaultmap);
            return JSON.toJSONString(resaultmap);

        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("失败");
        }

    }


    @Override
    public String selectSaleByDate(Date date) {
        try {
            List<SalesRecord>salesRecords=salesRecordMapper.selectSaleByDate(date);
            List<Map<String, String>> resaultmap = new ArrayList<>();
            for (SalesRecord salesRecord:salesRecords){
                resaultmap.add(getAll(salesRecord));
            }
            System.out.println(resaultmap);
            return JSON.toJSONString(resaultmap);

        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("失败");
        }

    }
    @Override
    public String update(int salId,int salRecCount,double salRecPrice,Date date,double salRecToTal) {
        try {
            SalesRecord salesRecord=salesRecordMapper.selectByPrimaryKey(salId);
            Inventory inventory=inventoryMapper.selectByPrimaryKey(salesRecord.getSalRecIvtrId());
            if(salesRecord.getSalRecCount()-salRecCount<0){
                inventory.setIvtrCount(inventory.getIvtrCount()-(salRecCount-salesRecord.getSalRecCount()));
            }else {
                inventory.setIvtrCount(inventory.getIvtrCount()+(salesRecord.getSalRecCount()-salRecCount));
            }
            salesRecord.setSalRecCount(salRecCount);
            salesRecord.setSalRecTime(date);
            salesRecord.setSalRecPrice(salRecPrice);
            salesRecord.setSalRecTotal(salRecToTal);
            salesRecordMapper.updateByPrimaryKey(salesRecord);
            inventoryMapper.updateByPrimaryKey(inventory);
            return getAllSale();

        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString("失败");
        }

    }
    @Override
    public SalesRecord selectSaleById(Integer id) {

            return salesRecordMapper.selectByPrimaryKey(id);

    }
    @Override
    public String delete(Integer id) {
        SalesRecord salesRecord=salesRecordMapper.selectByPrimaryKey(id);
        Inventory inventory=inventoryMapper.selectByPrimaryKey(salesRecord.getSalRecIvtrId());
        inventory.setIvtrCount(inventory.getIvtrCount()+salesRecord.getSalRecCount());
        inventoryMapper.updateByPrimaryKey(inventory);
        salesRecordMapper.deleteByPrimaryKey(id);
        return getAllSale();

    }

}
